<?php 
if ($this->request['action'] != 'list') return;
?>
<style type="text/css" template='<?php echo basename(__FILE__); ?>'>
#hide-controls th {
	color: #fff;
	text-decoration: underline;
	cursor: pointer;
}

#hide-controls th:hover {
	text-decoration: none;
}
</style>
<script type='text/javascript' template='<?php echo basename(__FILE__); ?>'>
$(function() {
	$table = $("#crud_list_items");
	$thead = $table.find("thead");
	$count = $table.find("tr:first").find("th").length;

	$controls = "<tr id='hide-controls'>";
	for ($i = 1; $i < $count; $i++) {
		$controls += "<th>Свернуть колонку</th>";
	}
	$controls += "<th class='unhide'>Развернуть все (<span id='hide_count'></span>)</th>";
	$controls += "</tr>";
	$thead.prepend($controls);

	hideControlIndex();

	var $cook = $.cookie('hideControls');
	if (!$cook) $cook = '';
	var $request = new Request('admin', 'crud', 'list');
	var $paramTable = $request.getParam("table");
	$("#hide-controls").live('click', function(event) {
		$target = $(event.target);
		if ($target.hasClass("unhide")) {
			$table.find("th, td").css("display", "");
			var $regexp = new RegExp($paramTable+':[^,]+,');
			while($regexp.test($cook)) {
				$cook = $cook.replace($regexp, '');
			}
			setCookie('hideControls', $cook, "01-Jan-2012", '/');
			$("#hide_count").text(0);
			return false;
		}

		$class = $target.attr("class");
		$('.'+$class).hide(100);

		$cook += $paramTable+':'+$class+',';
		setCookie('hideControls', $cook, "01-Jan-2012", '/');
		var $hideCount = $("#hide_count");
		$hideCount.text(parseInt($hideCount.text()) + 1);
		
		return false;
	});
});

function hideControlIndex() {
	$("#crud_list_items").find("tr").each(function() {
		$(this).find("td, th").each(function($i){
			var $class = "item"+$i;
			$(this).addClass($class);
		});
	});

		var $cook = getCookie('hideControls');
		if (!$cook) $cook = '';
		var $request = new Request('admin', 'crud', 'list');
		var $paramTable = $request.getParam("table");

		var $forHide = new Array();
		var $regexp = new RegExp($paramTable+':([^,]+),');
		while ($regexp.test($cook)) {
			var $match = $cook.match($regexp);
			$cook = $cook.replace($regexp, '');
			$forHide.push('.'+$match[1]);
		}

		var $selector = $forHide.join(', ');
		$($selector).hide(100);
		$("#hide_count").text($forHide.length);
	
}

</script>